// ADCard.cpp: implementation of the CADCard class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "testadcard.h"
#include "ADCard.h"
#include "CardConfigDlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CADCardControl::CADCardControl()
{

}

CADCardControl::~CADCardControl()
{
	
}
CADCardControl* CADCardControl::m_instance = NULL;

CADCardControl* CADCardControl::Instance()
{
	if(NULL == m_instance)   
	{   
		m_instance = new CADCardControl();  
	}   
    return m_instance; 
}

BOOL CADCardControl::OpenADCard(CADCard** ppcard)
{
	LRESULT error_code;
	char szerror_msg[80];
	DWORD dw_device_id;
	error_code = DRV_SelectDevice(NULL, FALSE, &dw_device_id, (unsigned char*)szerror_msg);
	if (error_code != SUCCESS)
	{
		DRV_GetErrorMessage(error_code,(LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
		return FALSE;
    }

	if (NULL == *ppcard)
	{
		*ppcard = new CADCard(dw_device_id);
		if (!((*ppcard)->IsOpened()))
		{
			return FALSE;
		}
	}
	else 
	{
		if ((*ppcard)->IsOpened())
		{
			(*ppcard)->Close();
		}
		(*ppcard)->m_dwDeviceNum = dw_device_id;
		(*ppcard)->Open();
	}
	return TRUE;
}

BOOL CADCardControl::GetADTransData(CADCard* pcard, BYTE** pbuf, DWORD count)
{
	ASSERT(pcard);
	ASSERT(*pbuf);
	PVOID buffer;
	int data_length;
	if (pcard->m_ADCardParam.m_wDataType == 0)
	{
		buffer = (USHORT far *)GlobalLock(pcard->m_ADCardParam.m_hUserDataBuf);
		data_length = sizeof(USHORT);
	}
	else
	{
		buffer = (FLOAT far *)GlobalLock(pcard->m_ADCardParam.m_hUserDataBuf);
		data_length = sizeof(FLOAT);
	}
	memcpy(*pbuf, buffer, data_length * count);
	GlobalUnlock(pcard->m_ADCardParam.m_hUserDataBuf);
	return TRUE;
}

BOOL CADCardControl::BeginFAIIntTrans(CADCard* pcard, HANDLE event_get_data)
{
	ASSERT(pcard);
	if (pcard->IsOpened() && (!pcard->IsWorking()))
	{
		pcard->SetEventOutHandle(event_get_data);
		pcard->FAIIntStart();
		return TRUE;
	}	
	return FALSE;
}

BOOL CADCardControl::StopFAIIntTrans(CADCard* pcard)
{
	ASSERT(pcard);
	if (pcard->FAIIntStop(FALSE))
	{
		return TRUE;
	}
	return FALSE;
}

BOOL CADCardControl::GetCardGainList(CADCard* pcard, USHORT** ppgainbuf, DWORD count)
{
	ASSERT(pcard);
	ASSERT(*ppgainbuf);
	if (count < 16)
	{
		AfxMessageBox("the buffer lenght should be lager than 16");
		return FALSE;
	}
	memcpy((void*)(*ppgainbuf), pcard->m_wGain, sizeof(USHORT) * 16); 
	return TRUE;
}

DWORD CADCardControl::GetDeviceID(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_dwDeviceNum;
}

BOOL CADCardControl::IsCardOpened(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->IsOpened();
}

USHORT CADCardControl::GetCardGainCode(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wGainCode;
}

void CADCardControl::SetCardGainCode(CADCard* pcard, USHORT gaincode)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wGainCode = gaincode;
}

DWORD CADCardControl::GetCardPacerRate(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_dwPacerRate;
}

void CADCardControl::SetCardPacerRate(CADCard* pcard, DWORD pacerrate)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_dwPacerRate = pacerrate;
}

ULONG  CADCardControl::GetCardConvNum(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_ulConvNum;
}

void CADCardControl::SetCardConvNum(CADCard* pcard, ULONG convnum)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_ulConvNum = convnum;
}

USHORT CADCardControl::GetCardStartChl(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wStartChl;
}

void CADCardControl::SetCardStartChl(CADCard* pcard, USHORT startchl)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wStartChl = startchl;
}

USHORT CADCardControl::GetCardStopChl(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wStopChl;
}

void CADCardControl::SetCardStopChl(CADCard* pcard, USHORT stopchl)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wStopChl = stopchl;
}

USHORT CADCardControl::GetCardDataType(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wDataType;
}

void CADCardControl::SetCardDataType(CADCard* pcard, USHORT datatype)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wDataType = datatype;
}

USHORT CADCardControl::GetCardEvtFlag(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wEvtFlag;
}
	
void CADCardControl::SetCardEvtFlag(CADCard* pcard, USHORT evtflag)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wEvtFlag = evtflag;
}

USHORT CADCardControl::GetCardFifoEnable(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wFifoEnable;
}

void CADCardControl::SetCardFifoEnable(CADCard* pcard, USHORT fifoenable)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wFifoEnable = fifoenable;
}

USHORT CADCardControl::GetCardFifoSize(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wFifoSize;
}

void CADCardControl::SetCardFifoSize(CADCard* pcard, USHORT fifosize)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wFifoSize = fifosize;
}

USHORT CADCardControl::GetCardGainList(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wGainList;
}

void CADCardControl::SetCardGainList(CADCard* pcard, USHORT gainlist)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wFifoSize = gainlist;
}

USHORT CADCardControl::GetCardExtTrig(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wExtTrig;
}

void CADCardControl::SetCardExtTrig(CADCard* pcard, USHORT exttrig)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wExtTrig = exttrig;
}

USHORT CADCardControl::GetCardCyclicMode(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wCyclicMode;
}

void CADCardControl::SetCardCyclicMode(CADCard* pcard, USHORT cyclicmode)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wCyclicMode = cyclicmode;
}

USHORT CADCardControl::GetCardBufferMode(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wBufferMode;
}

void CADCardControl::SetCardBufferMode(CADCard* pcard, USHORT buffermode)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wBufferMode = buffermode;
}

USHORT CADCardControl::GetCardIntrCount(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->m_ADCardParam.m_wIntrCount;
}

void CADCardControl::SetCardIntrCount(CADCard* pcard, USHORT intrcount)
{
	ASSERT(pcard);
	pcard->m_ADCardParam.m_wIntrCount = intrcount;
}

BOOL CADCardControl::IsCardWorking(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->IsWorking();
}

BOOL CADCardControl::CardConfigure(CADCard* pcard)
{
	ASSERT(pcard);
	CCardConfigDlg config_dlg;
	config_dlg.SelectCard(pcard);
	config_dlg.DoModal();
	return TRUE;
}

BOOL CADCardControl::GetCardFeature(CADCard* pcard, DEVFEATURES* pfeature)
{
	ASSERT(pcard);
	memcpy((void*)pfeature, (void*)(&pcard->m_DevFeatures), sizeof(DEVFEATURES));
	return TRUE;
}

LONG CADCardControl::GetCardFifoSizeFeature(CADCard* pcard)
{
	ASSERT(pcard);
	return pcard->GetFifoSize();
}

LRESULT CADCardControl::SetCardProperty(CADCard* pcard, USHORT nID, void* pBuffer, ULONG  dwLength)
{
	ASSERT(pcard);
	return DRV_DeviceSetProperty(pcard->m_DriverHandle, nID, pBuffer, dwLength);
	
}

LRESULT CADCardControl::GetCardProperty(CADCard* pcard, USHORT nID, void* pBuffer, ULONG*  pLength)
{
	ASSERT(pcard);
	return DRV_DeviceGetProperty(pcard->m_DriverHandle, nID, pBuffer, pLength);
}

//////////////////////////////////////////////////////////////////////////

CADCard::CADCard() : m_dwDeviceNum(0)
, m_DriverHandle(0)
, m_bWorking(FALSE)
, m_bOpened(FALSE)
, m_eventOut(NULL)
, m_hThreadHandle(NULL)
, m_dwThreadID(0)
{
	ZeroMemory((void*)m_wGain, sizeof(USHORT) * 16);
}

CADCard::CADCard(ULONG device_id) : m_dwDeviceNum(device_id)
, m_DriverHandle(0)
, m_bWorking(FALSE)
, m_bOpened(FALSE)
, m_eventOut(NULL)
, m_hThreadHandle(NULL)
, m_dwThreadID(0)
{
	ZeroMemory((void*)m_wGain, sizeof(USHORT) * 16);

// 	LRESULT error_code = DRV_DeviceOpen(m_dwDeviceNum, (LONG far *)&m_DriverHandle);
// 	char szerror_msg[80];
// 
// 	if (SUCCESS != error_code)
// 	{
// 		DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
// 		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
// 		return;
//     }
	Open();
}

CADCard::~CADCard()
{

}

BOOL CADCard::IsOpened()
{
	return m_bOpened;
}

BOOL CADCard::Open()
{
	LRESULT error_code = DRV_DeviceOpen(m_dwDeviceNum, (LONG far *)&m_DriverHandle);
	char szerror_msg[80];
	
	if (SUCCESS != error_code)
	{
		DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
		m_bOpened = FALSE;
		return FALSE;
    }

	PT_DeviceGetFeatures  pt_dev_features;
	pt_dev_features.buffer = (LPDEVFEATURES)&m_DevFeatures;
    pt_dev_features.size = sizeof(DEVFEATURES);
	error_code = DRV_DeviceGetFeatures(m_DriverHandle, (LPT_DeviceGetFeatures)&pt_dev_features);
	if (error_code != SUCCESS)
	{
		DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
		DRV_DeviceClose((LONG far *)&m_DriverHandle);
		m_bOpened = FALSE;
		return FALSE;
    }
	m_bOpened = TRUE;
	return TRUE;
}

void CADCard::Close()
{
	if (m_bOpened)
	{
		DRV_DeviceClose((LONG far *)&m_DriverHandle);
		m_bOpened = FALSE;
	}	
}

BOOL CADCard::EnableDrvEvent()
{
	LRESULT error_code;
	char szerror_msg[80];
	PT_EnableEvent pt_enable_event;
	pt_enable_event.EventType = ADS_EVT_INTERRUPT  |
								ADS_EVT_BUFCHANGE  |
								ADS_EVT_TERMINATED |
								ADS_EVT_OVERRUN;
	pt_enable_event.Enabled =m_ADCardParam.m_wEvtFlag;
	pt_enable_event.Count   = m_ADCardParam.m_wFifoEnable ? m_ADCardParam.m_wFifoSize : 1;
	error_code = DRV_EnableEvent(m_DriverHandle, (LPT_EnableEvent)&pt_enable_event);
	if (error_code != 0)
	{
		DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
		//DRV_DeviceClose((LONG far *)&DriverHandle);
		return FALSE;
	}
	return TRUE;
}

BOOL CADCard::FAIIntStart()
{
	LRESULT error_code;
	char szerror_msg[80];

	AllocDrvBuff();

	if (EnableDrvEvent())
	{
		PT_FAIIntStart pt_fai_int_start;
		pt_fai_int_start.buffer     = (USHORT far *)GlobalLock(m_ADCardParam.m_hDrvDataBuf);
		pt_fai_int_start.TrigSrc    = m_ADCardParam.m_wExtTrig;
		pt_fai_int_start.SampleRate = m_ADCardParam.m_dwPacerRate;
		pt_fai_int_start.chan   = m_ADCardParam.m_wStartChl;
		pt_fai_int_start.gain   = m_DevFeatures.glGainList[m_ADCardParam.m_wGainCode].usGainCde;
		pt_fai_int_start.count  = m_ADCardParam.m_ulConvNum;
		pt_fai_int_start.cyclic = m_ADCardParam.m_wCyclicMode;
		if (m_ADCardParam.m_wFifoEnable && m_ADCardParam.m_wFifoSize)
			pt_fai_int_start.IntrCount = m_ADCardParam.m_wFifoSize;
		else
			pt_fai_int_start.IntrCount = 1;
		
		error_code = DRV_FAIIntStart(m_DriverHandle, (LPT_FAIIntStart)&pt_fai_int_start);
		if (error_code != 0)
		{
			DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
			AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
			//DRV_DeviceClose((LONG far *)&DriverHandle);
			return FALSE;
		}
		m_bWorking = TRUE;
		if (m_ADCardParam.m_wEvtFlag)
		{
			m_hThreadHandle = CreateThread(0, 0,
				(LPTHREAD_START_ROUTINE) CADCard::ADIntTransThread,
				this, 0, (LPDWORD)&m_dwThreadID);
		}
		return TRUE;
	}
	return FALSE;
	
}

BOOL CADCard::FAIIntStop(BOOL b_terminated)
{
    LRESULT error_code;
	char szerror_msg[80];

	if (!b_terminated)
	{
		if ((error_code = DRV_FAITerminate(m_DriverHandle)) != 0)
		{
			DRV_GetErrorMessage(error_code,(LPSTR)szerror_msg);
			AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
			//DRV_DeviceClose((LONG far *)&m_DriverHandle);
			return FALSE;
		}
	}
    

	PT_FAITransfer pt_fai_transfer; 
	USHORT w_over_run   = 0;
	
    // Get real voltage of buffer from driver
	pt_fai_transfer.DataBuffer = (FLOAT far*)GlobalLock(m_ADCardParam.m_hUserDataBuf);
    pt_fai_transfer.ActiveBuf = 0;   // single buffer
    pt_fai_transfer.DataType = m_ADCardParam.m_wDataType;
    pt_fai_transfer.start    = 0;
    pt_fai_transfer.count    = m_ADCardParam.m_ulConvNum;
    pt_fai_transfer.overrun  = &w_over_run;
	
	m_bWorking = FALSE;
    if ((error_code = DRV_FAITransfer(m_DriverHandle,
        (LPT_FAITransfer)&pt_fai_transfer)) != 0)
    {
        DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
    }
	else
	{
		// Display data
		//DialogBox (hInstance, MAKEINTRESOURCE(IDD_DATA),
		//hWnd, lpfnShowDataDlgProc) ;

		SetEvent(m_eventOut);
	}

	GlobalUnlock(m_ADCardParam.m_hUserDataBuf);
	GlobalUnlock(m_ADCardParam.m_hDrvDataBuf);
	
    // Free buffer
    ReleaseDrvBuff();
		
		// Close driver
    //DRV_DeviceClose((LONG far *)&DriverHandle);
    return TRUE;
}

BOOL CADCard::AllocDrvBuff()
{
    if ((m_ADCardParam.m_hDrvDataBuf != NULL) || (m_ADCardParam.m_hUserDataBuf != NULL))
    {
        ReleaseDrvBuff();
    }
    m_ADCardParam.m_hDrvDataBuf = (USHORT far *)GlobalAlloc(GHND, sizeof(USHORT) * m_ADCardParam.m_ulConvNum);
	if (!m_ADCardParam.m_hDrvDataBuf)
	{
		AfxMessageBox(_T("Error on Alloc memory for DrvDataBuf"), MB_OK);
		return FALSE;
	}
	m_ADCardParam.m_hUserDataBuf =(FLOAT far *)GlobalAlloc(GHND, sizeof(FLOAT) * m_ADCardParam.m_ulConvNum);
	if (!m_ADCardParam.m_hUserDataBuf)
	{
		AfxMessageBox(_T("Error on Alloc memory for UserDataBuf"), MB_OK);
		return FALSE;
	}
	return TRUE;
}


void CADCard::ReleaseDrvBuff()
{
	if (m_ADCardParam.m_hDrvDataBuf != NULL)
	{
		GlobalFree((HGLOBAL) m_ADCardParam.m_hDrvDataBuf);
		m_ADCardParam.m_hDrvDataBuf = NULL;
	}
	if (m_ADCardParam.m_hUserDataBuf != NULL)
	{
		GlobalFree((HGLOBAL) m_ADCardParam.m_hDrvDataBuf);
		m_ADCardParam.m_hDrvDataBuf = NULL;
	}
}

BOOL CADCard::IsWorking()
{
    return m_bWorking;
}

UINT CADCard::ADIntTransThread(LPVOID pParam)
{
	CADCard* p_card = (CADCard*)pParam;

	USHORT usEventType;
    LRESULT error_code;
    ULONG ulIntCounter = 0;
    ULONG ulBfChgCounter = 0;
    ULONG ulOverrunCounter = 0;
	PT_CheckEvent pt_check_event;
	
    while(1)
    {
        // Check message
        pt_check_event.EventType = &usEventType;
		
		//
		// Shengkui modified, 2004/02/01
		// if external trigger source enabled, set the 
		// timeout interval to INFINITE
		if ( (p_card->m_ADCardParam.m_wExtTrig) || (p_card->m_ADCardParam.m_wCyclicMode) )
		{
			pt_check_event.Milliseconds = INFINITE;
		}
		else
		{
			pt_check_event.Milliseconds = 1000 * (p_card->m_ADCardParam.m_ulConvNum / p_card->m_ADCardParam.m_dwPacerRate) + 1000;
		}
		
        if ((error_code = DRV_CheckEvent( p_card->m_DriverHandle,
            (LPT_CheckEvent)&pt_check_event)) != 0)
        {
            AfxMessageBox("Check Event Error !",MB_OK);
            return 0;
        }
		
        // Process interrupt event
        if (usEventType & ADS_EVT_INTERRUPT)
		{
			ulIntCounter++;
            p_card->ADInterruptEventProc();
		}
		
        // Process buffer change event
        if (usEventType & ADS_EVT_BUFCHANGE)
		{
			ulBfChgCounter++;
            p_card->ADBufChangeEventProc();
		}
		
        // Process overrun event
        if (usEventType & ADS_EVT_OVERRUN)
		{
			ulOverrunCounter++;
            p_card->ADOverrunEventProc();
		}
		
        // Process terminate event
        if (usEventType & ADS_EVT_TERMINATED)
        {
            p_card->ADTerminateEventProc();			
            return 0;
        }
		//sprintf( szCounter, "Interrupt Counter = %4d\n", gulIntCounter );
		//TextOut( hdc, 300, 50, szCounter, 24 );  
		//sprintf( szCounter, "Buffer change Counter = %4d\n", gulBfChgCounter );
		//TextOut( hdc, 300, 70, szCounter, 28 );
		//sprintf( szCounter, "Overrun Counter = %4d\n", gulOverrunCounter );
		//TextOut( hdc, 300, 90, szCounter, 22 );
    }
    return 0l;
}

void CADCard::ADInterruptEventProc()
{

}

void CADCard::ADBufChangeEventProc()
{
	LRESULT error_code;
	char szerror_msg[80];

	PT_FAITransfer pt_fai_transfer; 
	PT_FAICheck pt_fai_check; 

	USHORT w_active_buf = 0;
	USHORT w_over_run   = 0;
	USHORT w_stopped   = 0;
	ULONG ul_retrieved = 0;
	USHORT w_half_Ready = 0;
	float a, *p;


	//pt_fai_check.DataBuffer = (FLOAT far  *)GlobalLock(m_ADCardParam.m_hUserDataBuf); 
	pt_fai_check.ActiveBuf = &w_active_buf;
    pt_fai_check.stopped   = &w_stopped;
    pt_fai_check.retrieved = &ul_retrieved;
    pt_fai_check.overrun   = &w_over_run;
    pt_fai_check.HalfReady = &w_half_Ready;
	
    if ((error_code = DRV_FAICheck(m_DriverHandle,
		(LPT_FAICheck)&pt_fai_check)) != 0)
    {
        DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
        AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
        return;
    }
	
	pt_fai_transfer.DataBuffer = (FLOAT far*)GlobalLock(m_ADCardParam.m_hUserDataBuf);
	p = (float*)pt_fai_transfer.DataBuffer;
	a = *p;
    if (w_half_Ready == 1)  // first ready
    {		
        pt_fai_transfer.ActiveBuf = 0;   // single buffer
        pt_fai_transfer.DataType = m_ADCardParam.m_wDataType;
        pt_fai_transfer.start    = 0;
        pt_fai_transfer.count    = m_ADCardParam.m_ulConvNum/2;
        pt_fai_transfer.overrun  = &w_over_run;
		
        if ((error_code = DRV_FAITransfer(m_DriverHandle,
            (LPT_FAITransfer)&pt_fai_transfer)) != 0)
        {
            DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
			AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
			GlobalUnlock(m_ADCardParam.m_hUserDataBuf);
            return ;
        }
    }
    else if (w_half_Ready == 2) // second half ready
    {
        pt_fai_transfer.ActiveBuf = 0;   // single buffer
        pt_fai_transfer.DataType = m_ADCardParam.m_wDataType;
        pt_fai_transfer.start    = m_ADCardParam.m_ulConvNum/2;
        pt_fai_transfer.count    = m_ADCardParam.m_ulConvNum/2;
        pt_fai_transfer.overrun  = &w_over_run;
		
        if ((error_code = DRV_FAITransfer(m_DriverHandle,
            (LPT_FAITransfer)&pt_fai_transfer)) != 0)
        {
            DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
			AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
			GlobalUnlock(m_ADCardParam.m_hUserDataBuf);
            return ;
        }
    }
	if (NULL != m_eventOut)
	{
		SetEvent(m_eventOut);
	}	
	p = (float*)pt_fai_transfer.DataBuffer;
	a = *p;
	GlobalUnlock(m_ADCardParam.m_hUserDataBuf);
    return;
}

void CADCard::ADOverrunEventProc()
{
	LRESULT error_code;
	char szerror_msg[80];
	// clear overrun
    if ((error_code = DRV_ClearOverrun(m_DriverHandle)) != 0)
    {
        DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
        return ;
    }
	
    return;
}
	
void CADCard::ADTerminateEventProc()
{
	FAIIntStop(TRUE);	
	return;
}

void CADCard::SetEventOutHandle(HANDLE hevent)
{
	//ASSERT(hevent != NULL);
	m_eventOut = hevent;
}

LONG CADCard::GetFifoSize()
{
	LRESULT error_code = 0;
	char szerror_msg[80] = {0};
	LONG fifo_size = 0;

	error_code = DRV_GetFIFOSize(m_DriverHandle, &fifo_size);
	if(error_code != SUCCESS)
	{
		m_ADCardParam.m_wFifoEnable = 0;
		DRV_GetErrorMessage(error_code, (LPSTR)szerror_msg);
		AfxMessageBox((LPCSTR)szerror_msg, MB_OK);
		return 0;
	}
	return fifo_size;
}

